package eraserhead.boundary;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import eraserhead.controller.LoginController;
import eraserhead.entity.User;


/** 
 * GUI for logging into a created DL Event.
 * @author Quanquan
 * @author Zhaolong Li
 */
public class LoginForm extends JFrame{
	private static final long serialVersionUID = -6092204902781227141L;

	boolean isModerator;
	
    private JButton cancelBtn;
    private JTextField idField;
    private JLabel idLabel;
    private JTextField nameField;
    private JLabel nameLabel;
    private JPasswordField passwordField;
    private JLabel passwordLabel;
    private JButton signInBtn;
	
    // Singleton 
    // static LoginForm form = null;
    
    /**
     * Creates a new LoginForm
     * @param isCreatingEvent True if the {@link User} logging in is the moderator
     */
	public LoginForm(boolean isCreatingEvent) {
		initComponents();
		if (isCreatingEvent) {
			isModerator = true;
			idField.setText("No need to enter event ID.");
			idField.setEditable(false);
		} else {
			isModerator = false;
		}
	}
	
	
	/*public static LoginForm getInstance(boolean isCreatingEvent){
		if (form == null){
			form = new LoginForm(isCreatingEvent);
			return form;
		}else return form;
	}*/
	
    private void initComponents() {

        nameField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        idField = new javax.swing.JTextField();
        idLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        signInBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(600, 480));
        setMinimumSize(new java.awt.Dimension(600, 480));
        setPreferredSize(new java.awt.Dimension(600, 480));

        nameField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        passwordField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        idField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        idLabel.setFont(new java.awt.Font("Felix Titling", 0, 14)); // NOI18N
        idLabel.setText("Event ID");

        nameLabel.setFont(new java.awt.Font("Felix Titling", 0, 14)); // NOI18N
        nameLabel.setText("Name");

        passwordLabel.setFont(new java.awt.Font("Felix Titling", 0, 14)); // NOI18N
        passwordLabel.setText("Password");

        signInBtn.setFont(new java.awt.Font("Felix Titling", 0, 14)); // NOI18N
        signInBtn.setText("Sign In");
        signInBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                new LoginController(LoginForm.this).process();
            }
        });

        cancelBtn.setFont(new java.awt.Font("Felix Titling", 0, 14)); // NOI18N
        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	new LoginController(LoginForm.this).cancel();
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordLabel)
                            .addComponent(nameLabel)
                            .addComponent(idLabel))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(signInBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(191, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(117, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(idLabel))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameLabel))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordLabel))
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(signInBtn)
                    .addComponent(cancelBtn))
                .addGap(110, 110, 110))
        );

        pack();
    }
    
    /**
     * Get the Event ID entered by user
     * @return The event ID
     */
    public String getEventID() {
    	return idField.getText();
    }
    
    /**
     * Get the name entered by user
     * @return The name
     */
    public String getName() {
    	return nameField.getText();
    }
    
    /**
     * Get the password entered by user
     * @return The password
     */
	public String getPassword() {
    	return passwordField.getPassword().toString();
    }
	
	/**
	 * Check if it's the moderator that's signing in
	 * @return True if moderator, false if regular user
	 */
	public boolean getIsModerator() {
		return isModerator;
	}
	
	/** 
	 * Change the Event Id for moderator
	 * @param id Event id to set
	 */
	public void changeID(String id){
		this.idField.setText(id);
		this.idField.setEditable(false);
	}
    
}
